
   {  Petrova Tatjana
         2012 / 12 / 09
         This program  draws 400 buttons and you can play with them.}
unit UnitPlayWthButtons;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;

    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  arr : array [0..19,0..19] of TButton;
  num,m,n: integer;
  s : TStringList;
  arr1 : array [0..99] of integer;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var i,j : integer;
begin
  for j:=0 to 19 do
  begin
    for i := 0 to 19 do
    begin
      s := TStringList.Create;
      m:=0;
      n:=0;
      arr[i][j]:= TButton.Create(Form1);
      arr[i][j].Width := 20;
      arr[i][j].Height := 20;
      arr[i][j].Parent := Form1;
      arr[i][j].Top := 20*(j mod 20);
      arr[i][j].Left := 20*(i mod 20);
      arr[i][j].OnClick := Button1Click;
   end;
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
var i,j,k,k1,a,b,c: integer;
begin
  for j:=0 to 19 do
  begin
    for i := 0 to 19 do
    begin
      if (Sender=arr[i][j]) then
      begin
        arr[i][j].Enabled := False;
        a := i;
        b := j;
        c:=0;
        if( num mod 2=0) then
        begin
          arr[i][j].Caption := 'X';
          while (arr[a][b].Caption = 'X')  do
          begin
            a:=a-1;
            c:=c+1;
          end;
          a := i+1;
          while (arr[a][b].Caption = 'X')  do
          begin
            a:=a+1;
            c:=c+1;
          end;
          if (c>=5) then
          begin
            Application.MessageBox('New game!', 'X have won!');
            for k:=0 to 19 do
              for k1 := 0 to 19 do
              begin
                arr[k][k1].Caption := '';
                arr[k][k1].Enabled := True;
              end;
            num := 0;
            m := m+1;
            Label1.Caption := 'X : '+  IntToStr(m);
            break;
          end;
          c :=0;
          a := i;
          b := j;
          while (arr[a][b].Caption = 'X')  do
          begin
            b:=b-1;
            c:=c+1;
          end;
          b := j+1;
          while (arr[a][b].Caption = 'X')  do
          begin
            b:=b+1;
            c:=c+1;
          end;
          if (c>=5) then
          begin
            Application.MessageBox('New game!', 'X have won!');
            for k:=0 to 19 do
              for k1 := 0 to 19 do
              begin
                arr[k][k1].Caption := '';
                arr[k][k1].Enabled := True;
              end;
            num := 0;
            m := m+1;
            Label1.Caption := 'X : '+  IntToStr(m);
            break;
          end;
          c :=0;
          a := i;
          b := j;
          while (arr[a][b].Caption = 'X')  do
          begin
            b:=b-1;
            a:=a-1;
            c:=c+1;
          end;
          b := j+1;
          a:= i+1;
          while (arr[a][b].Caption = 'X')  do
          begin
            b:=b+1;
            a := a+1;
            c:=c+1;
          end;
          if (c>=5) then
          begin
            Application.MessageBox('New game!', 'X have won!');
            for k:=0 to 19 do
              for k1 := 0 to 19 do
              begin
                arr[k][k1].Caption := '';
                arr[k][k1].Enabled := True;
              end;
            num := 0;
            m := m+1;
            Label1.Caption := 'X : '+  IntToStr(m);
            break;
          end ;
          c :=0;
          a := i;
          b := j;
          while (arr[a][b].Caption = 'X')  do
          begin
            b:=b+1;
            a:=a-1;
            c:=c+1;
          end;
          b := j-1;
          a:= i+1;
          while (arr[a][b].Caption = 'X')  do
          begin
            b:=b-1;
            a := a+1;
            c:=c+1;
          end;
          if (c>=5) then
          begin
            Application.MessageBox('New game!', 'X have won!');
            for k:=0 to 19 do
              for k1 := 0 to 19 do
              begin
                arr[k][k1].Caption := '';
                arr[k][k1].Enabled := True;
              end;
            num := 0;
            m := m+1;
            Label1.Caption := 'X : '+  IntToStr(m);
            break;
          end
        end else
        begin
          arr[i][j].Caption := '0';
          c :=0;
          a := i;
          b := j;
          while (arr[a][b].Caption = '0')  do
          begin
            a:=a-1;
            c:=c+1;
          end;
          a := i+1;
          while (arr[a][b].Caption = '0')  do
          begin
            a:=a+1;
            c:=c+1;
          end;
          if (c>=5) then
          begin
            Application.MessageBox('New game!', '0 have won!');
            for k:=0 to 19 do
              for k1 := 0 to 19 do
              begin
                arr[k][k1].Caption := '';
                arr[k][k1].Enabled := True;
              end;
            num := 0;
            n := n+1;
            Label2.Caption := '0 : '+  IntToStr(n);
            break;
          end;
          c :=0;
          a := i;
          b := j;
          while (arr[a][b].Caption = '0')  do
          begin
            b:=b-1;
            c:=c+1;
          end;
          b := j+1;
          while (arr[a][b].Caption = '0')  do
          begin
            b:=b+1;
            c:=c+1;
          end;
          if (c>=5) then
          begin
            Application.MessageBox('New game!', '0 have won!');
            for k:=0 to 19 do
             for k1 := 0 to 19 do
             begin
               arr[k][k1].Caption := '';
               arr[k][k1].Enabled := True;
             end;
            num := 0;
            n := n+1;
            Label2.Caption := '0 : '+  IntToStr(n);
            break;
          end;
          c :=0;
          a := i;
          b := j;
          while (arr[a][b].Caption = '0')  do
          begin
            b:=b-1;
            a:=a-1;
            c:=c+1;
          end;
          b := j+1;
          a:= i+1;
          while (arr[a][b].Caption = '0')  do
          begin
            b:=b+1;
            a := a+1;
            c:=c+1;
          end;
          if (c>=5) then
          begin
            Application.MessageBox('New game!', '0 have won!');
            for k:=0 to 19 do
             for k1 := 0 to 19 do
             begin
               arr[k][k1].Caption := '';
               arr[k][k1].Enabled := True;
             end;
            num := 0;
            n := n+1;
            Label2.Caption := '0 : '+  IntToStr(n);
            break;
          end;
          c :=0;
          a := i;
          b := j;
          while (arr[a][b].Caption = '0')  do
          begin
            b:=b-1;
            a:=a+1;
            c:=c+1;
          end;
          b := j-1;
          a:= i+1;
          while (arr[a][b].Caption = '0')  do
          begin
            b:=b+1;
            a := a-1;
            c:=c+1;
          end;
          if (c>=5) then
          begin
            Application.MessageBox('New game!', '0 have won!');
            for k:=0 to 19 do
             for k1 := 0 to 19 do
             begin
               arr[k][k1].Caption := '';
               arr[k][k1].Enabled := True;
             end;
            num := 0;
            n := n+1;
            Label2.Caption := '0 : '+  IntToStr(n);
            break;
          end;
        end;
      end;
    end;
  end;
  num:=num+1;
end;

end.



